// 本地数据保存、获取
// 主要用于跨页面数据共享
import { setItem, getItem, delItem, setForEverData, getForEverData } from '../common/env';


// 用户相关数据
const USER_ID = 'data_user_id';
const USER_OPENID = 'data_user_openid';
const USER_TOKEN = 'data_user_token';
const USER_INFO = 'data_user_info';

/**
 * 设置当前用户id、token、openid、info
 */
export const setUserId = (id) => {
    setItem(USER_ID, id);
};
export const getUserId = () => {
    return getItem(USER_ID);
};
export const setUserOpenId = (openId) => {
    setItem(USER_OPENID, openId);
};
export const getUserOpenId = () => {
    return getItem(USER_OPENID);
};
export const setUserToken = (token) => {
    setItem(USER_TOKEN, token);
};
export const getUserToken = () => {
    return getItem(USER_TOKEN);
};
export const setUserInfo = (info) => {
    setItem(USER_INFO, info);
};
export const getUserInfo = () => {
    return getItem(USER_INFO);
};
export const updateLocalUserInfo = (userInfo = {}) => {
    const baseInfo = getItem(USER_INFO) || {};
    const info = {
        ...baseInfo,
        ...userInfo,
    };
    setItem(USER_INFO, info);
    return info;
};


// 品牌(店铺)管理相关数据
const MANAGE_TARGET_ID = 'data_manage_target_id';
const MANAGE_TARGET_TYPE = 'data_manage_target_type';
const MANAGE_TARGET_INFO = 'data_manage_target_info';

/**
 * 设置当前管理类型type 品牌(店铺)id、info
 */
export const setMangeTargetId = (type, id) => {
    setForEverData(MANAGE_TARGET_TYPE, type);
    setForEverData(MANAGE_TARGET_ID, id);
};
export const getMangeTargetId = () => {
    return getForEverData(MANAGE_TARGET_ID);
};
export const getMangeTargetType = () => {
    return getForEverData(MANAGE_TARGET_TYPE);
};
export const setMangeTargetInfo = (info) => {
    setForEverData(MANAGE_TARGET_INFO, info);
};
export const getMangeTargetInfo = () => {
    return getForEverData(MANAGE_TARGET_INFO);
};
export const updateLocalMangeTargetInfo = (info = {}) => {
    const baseInfo = getForEverData(MANAGE_TARGET_INFO) || {};
    const bsInfo = {
        ...baseInfo,
        ...info,
    };
    setForEverData(MANAGE_TARGET_INFO, bsInfo);
    return bsInfo;
};


// 七牛上传token相关数据
const QINIU_TOKEN = 'data_qiniu_token';

/**
 * 设置七牛上传token
 * @param {String} token 
 */
export const setQiniuToken = (token) => {
    setItem(QINIU_TOKEN, token);
};

export const getQiniuToken = () => {
    return getItem(QINIU_TOKEN);
};

// cos上传token

// config配置信息
const CONFIG_INFO = 'data_config_info';

/**
 * 设置配置信息
 */
export const setConfigInfo = (config) => {
    setItem(CONFIG_INFO, config);
};

export const getConfigInfo = () => {
    return getItem(CONFIG_INFO);
};


// 售后相关数据
const AFTER_SALE_ORDER_INFO = 'after_sale_order_info';

/**
 * 设置需要售后的订单信息
 * @param {Object} orderInfo 订单信息
 */
export const setAfterSaleOrderInfo = (orderInfo) => {
    setItem(AFTER_SALE_ORDER_INFO, orderInfo);
};

export const getAfterSaleOrderInfo = () => {
    return getItem(AFTER_SALE_ORDER_INFO);
};


// 代言人分享数据
const SPOKESMAN_SHARE_INFO = 'data_spokesman_share_info_';

/**
 * 设置代言人分享数据(品牌、店铺id)
 */
export const setSpokesmanShareInfo = (shareInfo = {}) => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    const baseInfo = getForEverData(`${SPOKESMAN_SHARE_INFO}${manageTargetType}_${manageTargetId}`) || {};
    const info = {
        ...baseInfo,
        ...shareInfo,
    };
    setForEverData(`${SPOKESMAN_SHARE_INFO}${manageTargetType}_${manageTargetId}`, info);
};
export const getSpokesmanShareInfo = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${SPOKESMAN_SHARE_INFO}${manageTargetType}_${manageTargetId}`);
};


// 分销员分享数据
const DISTRIBUTOR_SHARE_INFO = 'data_distributor_share_info_';

/**
 * 设置分销员分享数据(品牌、店铺id)
 */
export const setDistributorShareInfo = (shareInfo = {}) => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    const baseInfo = getForEverData(`${DISTRIBUTOR_SHARE_INFO}${manageTargetType}_${manageTargetId}`) || {};
    const info = {
        ...baseInfo,
        ...shareInfo,
    };
    setForEverData(`${DISTRIBUTOR_SHARE_INFO}${manageTargetType}_${manageTargetId}`, info);
};
export const getDistributorShareInfo = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${DISTRIBUTOR_SHARE_INFO}${manageTargetType}_${manageTargetId}`);
};


// 操作记录flag
const CLOSE_WXMP_ATTENTION = 'date_close_wxmp_attention_';
const OPEN_SIGNIN_GUIDE_FLAG = 'data_open_signin_guide_flag_';
const OPEN_SPOKESMAN_GUIDE_FLAG = 'data_open_spokesman_guide_flag_';
const OPEN_DISTRIBUTOR_GUIDE_FLAG = 'data_open_distributor_guide_flag_';

/**
 * 关注公众号引导(品牌、店铺id)
 */
export const setCloseWxmpAttentionFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    setForEverData(`${CLOSE_WXMP_ATTENTION}${manageTargetType}_${manageTargetId}`, Date.now());
};
export const getCloseWxmpAttentionFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${CLOSE_WXMP_ATTENTION}${manageTargetType}_${manageTargetId}`);
};

/**
 * 首次引导开启每日签到(品牌、店铺id)
 */
export const setSigninGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    setForEverData(`${OPEN_SIGNIN_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`, Date.now());
};
export const getSigninGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${OPEN_SIGNIN_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`);
};

/**
 * 首次引导开启代言人招募(品牌、店铺id)
 */
export const setSpokesmanGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    setForEverData(`${OPEN_SPOKESMAN_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`, Date.now());
};
export const getSpokesmanGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${OPEN_SPOKESMAN_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`);
};

/**
 * 首次引导开启分销员招募(品牌、店铺id)
 */
export const setDistributorGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    setForEverData(`${OPEN_DISTRIBUTOR_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`, Date.now());
};
export const getDistributorGuideFlag = () => {
    const manageTargetType = getMangeTargetType();
    const manageTargetId = getMangeTargetId();
    return getForEverData(`${OPEN_DISTRIBUTOR_GUIDE_FLAG}${manageTargetType}_${manageTargetId}`);
};

// 订单来源信息下单使用(团购活动、抽奖、签到兑奖)
const ORDER_ORIGIN_INFO_PREFIX = 'order_origin_info_';

export const setOrderOriginInfo = (originId, originInfo) => {
    setItem(`${ORDER_ORIGIN_INFO_PREFIX}${originId}`, originInfo);
};
export const getOrderOriginInfo = (originId) => {
    return getItem(`${ORDER_ORIGIN_INFO_PREFIX}${originId}`);
};
export const clearOrderOriginInfo = (originId) => {
    delItem(`${ORDER_ORIGIN_INFO_PREFIX}${originId}`);
};
